package com.guigu.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigu.eduservice.dto.BusinessmanEquipmentDto;
import com.guigu.eduservice.entity.Businessman;
import com.guigu.eduservice.entity.BusinessmanEquipment;
import com.guigu.eduservice.entity.Equipment;
import com.guigu.eduservice.mapper.BusinessmanEquipmentMapper;
import com.guigu.eduservice.mapper.BusinessmanMapper;
import com.guigu.eduservice.mapper.EquipmentMapper;
import com.guigu.eduservice.service.BusinessmanEquipmentService;
import com.guigu.eduservice.vo.BusinessmanVo;
import com.guigu.eduservice.vo.EqVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class BusinessmanEquipmentServiceImpl extends ServiceImpl<BusinessmanEquipmentMapper, BusinessmanEquipment> implements BusinessmanEquipmentService {
    @Autowired
    private EquipmentMapper equipmentMapper;
    @Autowired
    private BusinessmanMapper businessmanMapper;
    @Autowired
    private BusinessmanEquipmentMapper businessmanEquipmentMapper;
    @Override
    public List<BusinessmanEquipmentDto> getTaskList(Integer businessmanId) {
        List<BusinessmanEquipmentDto> taskList = businessmanEquipmentMapper.getTaskList(businessmanId);
        return taskList;
    }
    @Override
    public boolean allocateEquipment(Long equipmentId, BusinessmanVo vo) {
        Equipment equipment = equipmentMapper.selectById(equipmentId);
        // 业务员与设备关联
        BusinessmanEquipment businessmanEquipment = new BusinessmanEquipment();
        businessmanEquipment.setBusinessmanId(Long.valueOf(vo.getBusinessmanId()));
        businessmanEquipment.setEquipmentId(equipmentId);
        if (equipment.getIsDetected() == 0){
            // 设备未检测
            businessmanEquipment.setResponsibilityType(0);
        }
        if (equipment.getStatus() == 3){
            // 设备已成交
            businessmanEquipment.setResponsibilityType(2);
        }
        businessmanEquipment.setAssignTime(LocalDateTime.now());
        businessmanEquipment.setStatus(1);

        return this.save(businessmanEquipment);
    }

    @Override
    public BusinessmanEquipment getBusinessmanEquipment(Long equipmentId) {
        QueryWrapper<BusinessmanEquipment> wrapper = new QueryWrapper<>();
        if (equipmentId != null && equipmentId != 0) {
            wrapper.eq("equipment_id", equipmentId);
        }
        BusinessmanEquipment businessmanEquipment = this.getOne(wrapper);
        if (businessmanEquipment == null){
            return null;
        }
        Businessman businessman = businessmanMapper.selectById(businessmanEquipment.getBusinessmanId());
        businessmanEquipment.setBusinessman(businessman);
        return businessmanEquipment;
    }
}
